package com.ycrlkj.android.modulespublic.common.net.transformer;

import android.util.Log;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.response.MResponse;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;

public class ResponseDataHandler<T> {

    private final MResponse<T> response;

    public ResponseDataHandler(MResponse<T> response) {
        this.response = response;
    }

    public ObservableSource<T> doWith() throws Exception {

        Integer code = response.getCode();
        String message = response.getMessage();
        T data = response.getData();

        Log.e("ming", "请求解析 code = " + code);
        Log.e("ming", "请求解析 message = " + message);
        Log.e("ming", "请求解析 data = " + data);

        if (code == 2000) {
            if(data == null){
                throw new ApiException(2000, "没有数据");
            }
            return Observable.just(data);
        }else{
            throw new ApiException(code, message);
        }

    }

}
